package com.bookmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bookmall.dao.IndexDao;
import com.bookmall.entity.Index;
import com.bookmall.util.DBUtil;

public class IndexDaoImpl implements IndexDao {

	@Override
	public boolean insert(Index t) throws SQLException {
		// TODO 姜宏
		Connection c = DBUtil.getConnection();
		String sql = "insert into index_tb(indextypeid,bookid,`rank`) values(?,?,?)";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, t.getIndextypeId());
		ps.setInt(2, t.getBookId());
		ps.setInt(3, t.getRank());
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean update(Index t) throws SQLException {
		// 得到连接
		Connection c = DBUtil.getConnection();
		// 写sql文
		String sql = "update index_tb set `rank`=? where indexid = ?";
		// 得到句柄
		PreparedStatement ps = c.prepareStatement(sql);
		// 设置参数
		ps.setInt(1, t.getRank());
		ps.setInt(2, t.getIndexId());
		// 执行
		int row = ps.executeUpdate();
		// 关闭
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public Index selectById(int id) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "select indexId,indexTypeId,bookId,`rank`,create_time,modify_time from index_tb where indexId=? and delete_flag=0  order by `rank` desc";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			Index i = new Index();
			i.setIndexId(rs.getInt(1));
			i.setIndextypeId(rs.getInt(2));
			i.setBookId(rs.getInt(3));
			i.setRank(4);
			i.setCreateTime(rs.getTimestamp(5).toLocalDateTime());
			if (rs.getTimestamp(6) != null) {
				i.setModifyTime(rs.getTimestamp(6).toLocalDateTime());
			}
			DBUtil.closeConnection(c, ps, rs);
			return i;
		} else {
			DBUtil.closeConnection(c, ps, rs);
			return null;
		}
	}

	@Override
	public List<Index> selectByPage(int page, int pageSize) throws SQLException {
		Connection c = DBUtil.getConnection();
		List<Index> index = new ArrayList<>();
		String sql = "select indexId,indexTypeId,bookId,`rank`,create_time,modify_time from index_tb where delete_flag=0 order by `rank` desc limit ?,?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, (page - 1) * pageSize);
		ps.setInt(2, pageSize);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			Index w = new Index();
			w.setIndexId(rs.getInt(1));
			w.setIndextypeId(rs.getInt(2));
			w.setBookId(rs.getInt(3));
			w.setRank(rs.getInt(4));
			w.setCreateTime(rs.getTimestamp(5).toLocalDateTime());
			if (rs.getTimestamp(6) != null) {
				w.setModifyTime(rs.getTimestamp(6).toLocalDateTime());
			}
			index.add(w);
		}
		DBUtil.closeConnection(c, ps, rs);
		return index.isEmpty() ? Collections.emptyList() : index;
	}

	@Override
	public boolean delete(int id) throws SQLException {
		// 得到连接
		Connection c = DBUtil.getConnection();
		// 写sql文
		String sql = "update index_tb set delete_flag = 1 where indexid=? and delete_flag = 0";
		// 得到句柄
		PreparedStatement ps = c.prepareStatement(sql);
		// 设置参数？
		ps.setInt(1, id);
		// 执行
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}
}